//
//  HomeViewController.m
//  Doc Bao
//
//  Created by sieg on 9/22/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "HomeViewController.h"
#import "Post.h"
#import "PostExtensions.h"
#import "News.h"
#import "NewsExtensions.h"
#import "NewCell.h"

@implementation HomeViewController

@synthesize tblView;
@synthesize newsArray;

#pragma mark -
#pragma mark View lifecycle


- (void)viewDidLoad 
{
  [super viewDidLoad];
  newsArray = [[NSMutableArray alloc] initWithArray:[News getNews]];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
 
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
 
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return [newsArray count];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  NSArray *listPostOfNew = [Post getPostsWithNews:[[[newsArray objectAtIndex:section] Id] intValue]];
  return [listPostOfNew count] > 0 ? [listPostOfNew count] : 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
	return 78;
}

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
	
	return 40.0;
	
}

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  
    // create the parent view
	UIView * customSectionView = [[UIView alloc] 
                                initWithFrame:CGRectMake(0.0, 
                                                         -5, 
                                                         self.tableView.frame.size.width, 
                                                         [self tableView:tableView heightForHeaderInSection:section])];
	customSectionView.backgroundColor = [[UIColor colorWithRed:0.206 
                                                       green:0.261 
                                                        blue:0.247 
                                                       alpha:1.000] 
                                       colorWithAlphaComponent:0.9];
	
    // create the label
	UILabel * headerLabel = [[UILabel alloc] 
                           initWithFrame:CGRectMake(10, 
                                                    0, 
                                                    300, 
                                                    customSectionView.frame.size.height)];
	headerLabel.backgroundColor = [UIColor clearColor];
	headerLabel.opaque = NO;
	headerLabel.textColor = [UIColor whiteColor];
	headerLabel.highlightedTextColor = [UIColor whiteColor];
	headerLabel.font = [UIFont fontWithName:@"Georgia" size:14];
	headerLabel.text = [[newsArray objectAtIndex:section] Name];
	
    // package and return
	[customSectionView addSubview:headerLabel];
	[headerLabel release];
	return [customSectionView autorelease];
	
}

  // Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
  static NSString *CellIdentifier = @"NewCell";
    
  NewCell *cell = (NewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	
  if (cell == nil) {
    cell = [[[NewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
		NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"NewCell" owner:nil options:nil];
		
		for(id currentObject in topLevelObjects)
		{
			if([currentObject isKindOfClass:[NewCell class]])
			{
				cell = (NewCell *)currentObject;
				break;
			}
		}
  }
    
    // Configure the cell...
  
  NSArray *postWithNewIdArray = [Post getPostsWithNews:[[[newsArray objectAtIndex:indexPath.section] Id] intValue]];
  if ([postWithNewIdArray count] > 0) {
    Post *aPost = [postWithNewIdArray objectAtIndex:indexPath.row];
    cell.title.text = aPost.Title;
    cell.content.text = aPost.Description;
  }
  else {
    cell.title.text = @"Khong co news moi";
    cell.content.text = @" ";
  }
  
  return cell;
}


#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
    /*
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
    // ...
    // Pass the selected object to the new view controller.
    [self.navigationController pushViewController:detailViewController animated:YES];
    [detailViewController release];
    */
}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


- (void)dealloc {
  [newsArray release];
  [tblView release];
  [super dealloc];
}


@end

